//
//  ViewController.m
//  BallLight
//
//  Created by Gaomingyang on 2021/4/12.
//  Copyright © 2021 gmy. All rights reserved.
//

#import "ViewController.h"
#import "BallLightRender.h"

@interface ViewController ()
@property (nonatomic, strong) BallLightRender *objRender;
@property (nonatomic, strong) GLKView *glkView;

@property (nonatomic,strong)UILabel     *lightPositionXLbl;
@property (nonatomic,strong)UILabel     *lightPositionYLbl;
@property (nonatomic,strong)UILabel     *lightPositionZLbl;

@property (nonatomic,strong)UITextField *lightPositionXTF;
@property (nonatomic,strong)UITextField *lightPositionYTF;
@property (nonatomic,strong)UITextField *lightPositionZTF;

@property (nonatomic,assign)NSInteger   lightPositionXValue;
@property (nonatomic,assign)NSInteger   lightPositionYValue;
@property (nonatomic,assign)NSInteger   lightPositionZValue;

@property (nonatomic,strong)UIButton *refreshBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self commonInit];
}

-(void)commonInit{
    ///光源位置，x，y，z 这里根据GLKView 的bounds 来设置，最终opengl会归一化
    //中心点为 0，0，0 。
    //左下角为 -width/2,-height/2
    // z 的正方向是向屏幕里面
    self.lightPositionXValue = 200;
    self.lightPositionYValue = 400;
    self.lightPositionZValue = -100;
    
    [self initOpengl];
    [self addMySubViews];
    [self addMyContraints];
}
- (void)initOpengl{
    //1.创建context
    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    //是指当前的context
    [EAGLContext setCurrentContext:context];
    //2.创建GLKView 并设置代理
    self.glkView = [[GLKView alloc] initWithFrame:self.view.bounds context:context];
    self.glkView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.glkView];
    self.objRender = [[BallLightRender alloc] initWithGLKView:self.glkView];
    self.objRender.lightPositionX = self.lightPositionXValue;
    self.objRender.lightPositionY = self.lightPositionYValue;
    self.objRender.lightPositionZ = self.lightPositionZValue;
    [self.objRender setDisplay];

}

-(void)addMySubViews{
    [self.view addSubview:self.lightPositionXLbl];
    [self.view addSubview:self.lightPositionYLbl];
    [self.view addSubview:self.lightPositionZLbl];
    
    [self.view addSubview:self.lightPositionXTF];
    [self.view addSubview:self.lightPositionYTF];
    [self.view addSubview:self.lightPositionZTF];
    
    [self.view addSubview:self.refreshBtn];
}
-(void)addMyContraints{
    self.lightPositionXLbl.frame = CGRectMake(15, 100, 60, 30);
    self.lightPositionYLbl.frame = CGRectMake(15, 140, 60, 30);
    self.lightPositionZLbl.frame = CGRectMake(15, 180, 60, 30);
    
    self.lightPositionXTF.frame = CGRectMake(80, 100, 100, 30);
    self.lightPositionYTF.frame = CGRectMake(80, 140, 100, 30);
    self.lightPositionZTF.frame = CGRectMake(80, 180, 100, 30);
    
    self.refreshBtn.frame = CGRectMake(80, 220, 100, 30);
}

#pragma mark -- clicks
-(void)refreshClick:(UIButton *)btn{
    
    NSString *xStr = self.lightPositionXTF.text ;
    NSString *yStr = self.lightPositionYTF.text ;
    NSString *zStr = self.lightPositionZTF.text ;
    NSLog(@" 点击了刷新 xStr:%@ - yStr:%@ - zStr:%@ - xLength:%lu",xStr,yStr,zStr,(unsigned long)xStr.length);
    if (xStr == nil || [xStr isEqualToString:@""] || xStr.length == 0) {
        NSLog(@" x 为空");
        self.lightPositionXValue = 0;
    }else {
        self.lightPositionXValue = xStr.integerValue;
    }
    
    if (yStr == nil || [yStr isEqualToString:@""] || yStr.length == 0) {
        NSLog(@" y 为空");
        self.lightPositionYValue = 0;
    }else {
        self.lightPositionYValue = yStr.integerValue;
    }
    
    if (zStr == nil || [zStr isEqualToString:@""] || zStr.length == 0) {
        NSLog(@" z 为空");
        self.lightPositionZValue = 0;
    }else {
        self.lightPositionZValue = zStr.integerValue;
    }
    self.objRender.lightPositionX = self.lightPositionXValue;
    self.objRender.lightPositionY = self.lightPositionYValue;
    self.objRender.lightPositionZ = self.lightPositionZValue;
    [self.objRender setDisplay];
}

#pragma mark -- lazy load
- (UILabel *)lightPositionXLbl{
    if (_lightPositionXLbl == nil) {
        _lightPositionXLbl = [UILabel new];
        _lightPositionXLbl.textColor = [UIColor orangeColor];
        _lightPositionXLbl.font = [UIFont systemFontOfSize:18];
        _lightPositionXLbl.text = @"x值";
    }
    return _lightPositionXLbl;
}
- (UILabel *)lightPositionYLbl{
    if (_lightPositionYLbl == nil) {
        _lightPositionYLbl = [UILabel new];
        _lightPositionYLbl.textColor = [UIColor orangeColor];
        _lightPositionYLbl.font = [UIFont systemFontOfSize:18];
        _lightPositionYLbl.text = @"y值";
    }
    return _lightPositionYLbl;
}
- (UILabel *)lightPositionZLbl{
    if (_lightPositionZLbl == nil) {
        _lightPositionZLbl = [UILabel new];
        _lightPositionZLbl.textColor = [UIColor orangeColor];
        _lightPositionZLbl.font = [UIFont systemFontOfSize:18];
        _lightPositionZLbl.text = @"z值";
    }
    return _lightPositionZLbl;
}
- (UITextField *)lightPositionXTF{
    if (_lightPositionXTF == nil) {
        _lightPositionXTF = [UITextField new];
        _lightPositionXTF.backgroundColor = [UIColor greenColor];
    }
    return _lightPositionXTF;
}
- (UITextField *)lightPositionYTF{
    if (_lightPositionYTF == nil) {
        _lightPositionYTF = [UITextField new];
        _lightPositionYTF.backgroundColor = [UIColor greenColor];
    }
    return _lightPositionYTF;
}
- (UITextField *)lightPositionZTF{
    if (_lightPositionZTF == nil) {
        _lightPositionZTF = [UITextField new];
        _lightPositionZTF.backgroundColor = [UIColor greenColor];
    }
    return _lightPositionZTF;
}
- (UIButton *)refreshBtn{
    if (_refreshBtn == nil) {
        _refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _refreshBtn.backgroundColor = [UIColor redColor];
        [_refreshBtn setTitle:@"刷新" forState:UIControlStateNormal];
        [_refreshBtn addTarget:self action:@selector(refreshClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _refreshBtn;
}

@end
